<?php

namespace app\shop\controller\proxy;

use app\shop\controller\Controller;
use app\shop\model\proxy\User as ProxyModel;

class Proxy extends Controller
{
    /**
     * 店员列表
     */
    public function index()
    {
        // 供应商列表
        $model = new ProxyModel;
        $list = $model->getList($this->postData());
        return $this->renderSuccess('', compact('list'));
    }

    public function add()
    {
        $model = new ProxyModel;
        // 新增记录
        if ($model->add($this->postData())) {
            return $this->renderSuccess('', '添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    public function edit($proxy_id)
    {
        $model = ProxyModel::detail($proxy_id);
        if ($this->request->isGet()) {
            $authority = $model->toarray();
            jsonRecursive($authority);
            $model->regions = $model->getRegions();
            $model->password="";
            $model->authority=json_decode($authority['authority'],true);
            return $this->renderSuccess('', compact('model'));
        }
        $postData = $this->postData();
        $postData['proxy']['authority'] = json_encode($postData['proxy']['authority'],true);
        if ($model->edit($postData)) {
            return $this->renderSuccess('', '更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    public function recycle($proxy_id, $status)
    {
        // 商品详情
        $model = ProxyModel::detail($proxy_id);
        if (!$model->setRecycle($status)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    public function delete($proxy_id)
    {
        // 店员详情
        $model = ProxyModel::detail($proxy_id);
        if (!$model->setDelete()) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('', $model->getError() ?: '删除成功');
    }
}